<?php

require_once 'common/conn.php';
require_once 'common/common.php';

requestURL($_SERVER['REQUEST_URI']);

$result = array();

if (isset($_GET['usercode'])) {
    $usercode = $_GET['usercode'];
}
if (isset($_GET['type'])) {
    $type = $_GET['type'];
} else {
    $type = "";
}
if (isset($_GET['flashcode'])) {
    $flashcode = $_GET['flashcode'];
} else {
    $flashcode = "";
}
if (isset($_GET['amount'])) {
    $amount = $_GET['amount'];
}

if (isset($usercode) && isset($amount)) {
    $db = new DBC();
    $usercode = trim($usercode);
    $sql = "SELECT gold FROM t_device WHERE devicecode = '" . $usercode . "'";
    $data = $db->fetchData($sql);
    if (checkArrayNotEmpty($data)) {
        if ($amount < 0 && $data[0]['gold'] < abs($amount)) {
            $result['errorNo'] = "3";
            $result['errorMsg'] = "金币数量不够";
        } else {
            $sql = "INSERT INTO t_gold_record(usercode,type,flashcode,amount,createTime) VALUES('" . $usercode . "','" . $type . "','" . $flashcode . "'," . $amount . ",now())";
            if ($db->execute($sql)) {
                $result['errorNo'] = "0";
                $result['errorMsg'] = "";
                $sql = "UPDATE t_device SET gold = gold + " . $amount . " WHERE devicecode = '" . $usercode . "'";
                $db->execute($sql);
            } else {
                $result['errorNo'] = "1";
                $result['errorMsg'] = "更新金币错误";
            }
        }
    } else {
        $result['errorNo'] = "3";
        $result['errorMsg'] = "无UID是：" . $usercode . " 的用户";
    }
} else {
    $result['errorNo'] = "2";
    $result['errorMsg'] = "参数不完整";
}

echo json_encode($result);
?>
